using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 拉扯Buff - 将目标拉向施法���
/// </summary>
public class PullBuff : BuffBase
{
    private Vector3 m_PullDirection;
    private float m_PullSpeed;
    private float m_PullDistance;
    private bool m_IsPulling;

    public PullBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is PullData pullData)
        {
            m_PullSpeed = pullData.PullSpeed;
            m_PullDistance = pullData.PullDistance;
        }
        else
        {
            m_PullSpeed = 8.0f; // 默认拉扯速度
            m_PullDistance = 2.0f; // 默认拉扯距离
        }
        
        // 计算拉扯方向（朝向施法者）
        if (Caster != null)
        {
            m_PullDirection = (Caster.transform.position - Target.transform.position).normalized;
        }
        else
        {
            m_PullDirection = Vector3.left; // 默认向左拉扯
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        m_IsPulling = true;
        
        // 播放拉扯特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        // 设置动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("pulled", true);
        }
        
        Logger.Log($"{Target.name} 被拉扯向 {Caster.name}");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        if (m_IsPulling && Target.MoveComponent != null)
        {
            // 计算目标位置
            Vector3 currentPos = Target.transform.position;
            Vector3 targetPos = currentPos + m_PullDirection * m_PullSpeed * dt;
            
            // 检查是否达到拉扯距���
            if (Caster != null)
            {
                float distanceToCaster = Vector3.Distance(targetPos, Caster.transform.position);
                if (distanceToCaster <= m_PullDistance)
                {
                    // 停止拉扯
                    m_IsPulling = false;
                    return;
                }
            }
            
            // 执行拉扯移动
            Target.transform.position = targetPos;
        }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        m_IsPulling = false;
        Logger.Log($"{Target.name} 拉扯效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        m_IsPulling = false;
        
        // 恢复动画状���
        if (Target.Animator != null)
        {
            Target.Animator.SetBool("pulled", false);
        }
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Pull"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 拉扯效果不能叠加，只能刷新持续时���
    }
}
